package pers.cz.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.cz.enums.CommonCode;
import pers.cz.filter.VerifyCodeGenerator;
import pers.cz.model.CaptchaImage;
import pers.cz.model.VerifyCode;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.UUID;

/**
 * @program: PostGirl-panent
 * @description: 获取验证码
 * @author: Cheng Zhi
 * @create: 2022-06-20 08:43
 **/
@RestController
@RequestMapping("/api/verifyCodeController")
public class VerifyCodeController {

    @Autowired
    JedisPool jedisPool;

    @Value("${verifyCode.expireTime}")
    private String expireTime;

    @Value("${verifyCode.type}")
    private String verifyCodeType;

    static BASE64Encoder encoder = new sun.misc.BASE64Encoder();

    @Resource
    private DefaultKaptcha captchaProducer;

    public VerifyCode code(HttpServletResponse resp) throws IOException {
        VerifyCodeGenerator vc = new VerifyCodeGenerator();
        BufferedImage image = vc.getImage();
        String text = vc.getText();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // write the data out
        ImageIO.write(image, "jpg", out);
        byte[] bytes = out.toByteArray();
        String strImage = encoder.encodeBuffer(bytes).trim();

        String random = UUID.randomUUID().toString();
        String key = CommonCode.VERIFYPRI + random;
        try (Jedis resource = jedisPool.getResource();) {
            resource.set(key, text);
            resource.expire(key, Integer.valueOf(expireTime));
        }

        VerifyCode verifyCode = new VerifyCode();
        verifyCode.setCode(strImage);
        verifyCode.setUuid(random);
        return verifyCode;
    }

    @GetMapping("/kaptcha")
    public VerifyCode getVerifyCode(HttpServletResponse response) throws IOException {

        if ("kaptcha".equals(verifyCodeType)) {
            return kaptcha(response);
        } else {
            return code(response);
        }
    }


    public VerifyCode kaptcha(HttpServletResponse response) throws IOException {

/*        response.setDateHeader("Expires",0);
        response.setHeader("Cathe-Control","no-store,no-cahe,must-revalidate");
        response.addHeader("Cache-Control","post-check=0,pre-check=0");
        response.setHeader("Pragma","no-cache");
        response.setContentType("image/jpeg");*/

        String capText = captchaProducer.createText();
        /* 设置验证字符串 和过期时间2分钟*/
        CaptchaImage captchaImageVO = new CaptchaImage(capText, 2*60);
        /*根据文字生成图片*/
        BufferedImage  bufferedImage = captchaProducer.createImage(capText);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // write the data out
        ImageIO.write(bufferedImage, "jpg", out);
        byte[] bytes = out.toByteArray();
        String strImage = encoder.encodeBuffer(bytes).trim();

        String random = UUID.randomUUID().toString();
        String key = CommonCode.VERIFYPRI + random;
        try (Jedis resource = jedisPool.getResource();) {
            resource.set(key, capText);
            resource.expire(key, Integer.valueOf(expireTime));
        }

        VerifyCode verifyCode = new VerifyCode();
        verifyCode.setCode(strImage);
        verifyCode.setUuid(random);
        return verifyCode;
    }
}
